﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Spring.Context;
using Spring.Context.Support;

namespace OcenyOpisowe
{
	public sealed class SpringControllerFactory : DefaultControllerFactory
	{
		private static readonly IApplicationContext SpringContext = ContextRegistry.GetContext();

		protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
		{
			if (controllerType != null)
			{
				IDictionary objectsOfType = SpringContext.GetObjectsOfType(controllerType);
				if (objectsOfType.Count > 0)
				{
					return (IController)objectsOfType.Cast<DictionaryEntry>().First().Value;
				}
			}
			return base.GetControllerInstance(requestContext, controllerType);
		}
	}
}